package com.news.servlet;


import com.news.pojo.User;
import com.news.service.UserService;
import com.news.service.impl.UserServiceImpl;
import com.news.vo.LoginUser;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 用户的事务控制层
 */
//配置路径
@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
    private UserService userService= new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
        //2.设置响应内容格式
        response.setContentType("text/html;charset=UTF-8");
        //请求控制路由
        String opr = request.getParameter("opr");
        switch(opr){
            case "login":{
                login(request,response);
            }
        }
    }

    /**
     * 用户登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void login(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
        //1.接受请求参数
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        User user = new User();
        user.setUname(uname);
        user.setUpwd(upwd);
        //2.模拟service的业务
        User loginUser = userService.login(user);
        HttpSession session = request.getSession();//获取本次会话
        if(loginUser ==  null){//登录失败
            session.setAttribute("login_error","账号或密码不匹配");
            response.sendRedirect("index");
        }else{
            if(loginUser.getUrole() != 0){//登录成功
                LoginUser login = new LoginUser();
                login.setIp("客户端的IP地址：");
                login.setType("电脑端");
                login.setUser(loginUser);
                session.setAttribute("login",login);
                session.setMaxInactiveInterval(60*15);
                //添加在线人数
                int count = 0;
                ServletContext application = request.getServletContext();//获取全局上下文
                if(application.getAttribute("loginCounts") != null){
                     count = (Integer) application.getAttribute("loginCount");
                }
                //cookie保存
            }
            if(loginUser.getUrole() == 1){//普通用户
                response.sendRedirect("/index.jsp");
            }else if(loginUser.getUrole() == 2){//超管
                response.sendRedirect("/admin/admin.jsp");
            }
        }
    }
    public void loginOut(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.invalidate();
        //减少在线人数
        ServletContext application = this.getServletContext();
        int count = 0;
        if(application.getAttribute("loginCount") != null){
            count = (Integer) application.getAttribute("loginCount");
        }
        application.setAttribute("loginCount",--count);
        response.sendRedirect("/index.jsp");
    }
}
